package org.meetunexpectedly.controller;

import org.meetunexpectedly.config.QiNiuConfig;
import org.meetunexpectedly.entity.user.Favorites;
import org.meetunexpectedly.entity.video.Type;
import org.meetunexpectedly.entity.vo.*;
import org.meetunexpectedly.holder.UserHolder;
import org.meetunexpectedly.service.user.FavoritesService;
import org.meetunexpectedly.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 用户控制类
 * @Author: pupil
 * @Date: 2024/08/11 下午 6:41
 */
@RestController
@RequestMapping("/meetunexpectedly/customer")
public class CustomerController {

    @Autowired
    private UserService userService;

    @Autowired
    private QiNiuConfig qiNiuConfig;

    @Autowired
    private FavoritesService favoritesService;

    /**
     * 获取个人信息
     *
     * @param userId 用户id
     * @return
     */
    @GetMapping("/getInfo/{userId}")
    public Result getInfo(@PathVariable Long userId) {
        return Result.ok().data(userService.getInfo(userId));
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/getInfo")
    public Result getDefaultInfo() {
        return Result.ok().data(userService.getInfo(UserHolder.get()));
    }

    /**
     * 获取粉丝列表
     *
     * @param basePage 基础分页信息
     * @param userId   用户Id
     * @return
     */
    @GetMapping("/fans")
    public Result getFans(BasePage basePage, Long userId) {
        return Result.ok().data(userService.getFans(basePage, userId));
    }

    /**
     * 获取关注列表
     *
     * @param basePage 基础分页信息
     * @param userId   用户Id
     * @return
     */
    @GetMapping("/follows")
    public Result getFollows(BasePage basePage, Long userId) {
        return Result.ok().data(userService.getFollows(basePage, userId));
    }


    /**
     * 关注/取关
     *
     * @param followsUserId 关注人员id
     * @return
     */
    @PostMapping("/follows")
    public Result follows(@RequestParam Long followsUserId) {
        return Result.ok().message(userService.follows(followsUserId) ? "已关注" : "已取关");
    }


    /**
     * 用户停留时长修改模型
     *
     * @param model 模型
     * @return
     */
    @PostMapping("/updateUserModel")
    public Result updateUserModel(@RequestBody Model model) {
        System.out.println("模型标签:"+ model.getLabel() +" 标签概率:"+model.getScore() );
        Double probability = model.getScore();
        if (probability == -0.5 || probability == 1.0) {
            UserModel userModel = new UserModel();
            userModel.setUserId(UserHolder.get());
            String[] labels = model.buildLabels();
            if(labels.length > 1){
                List<Model> models = new ArrayList<>();
                for (String label : labels){
                    Model newModel =  new Model();
                    newModel.setVideoId(model.getVideoId());
                    newModel.setScore(probability);
                    newModel.setLabel(label);
                    models.add(newModel);
                }
                userModel.setModels(models);
            }else {
                userModel.setModels(Collections.singletonList(model));
            }
            userService.updateUserModel(userModel);
        }
        return Result.ok();
    }


    /**
     * 订阅分类
     *
     * @param types 分类id，分类为空也能调用执行
     * @return
     */
    @PostMapping("/subscribe")
    public Result subscribe(@RequestParam(required = false) String types) {
        String msg = userService.subscribe(types);
        return Result.ok().message(msg);
    }

    /**
     * 获取用户订阅的分类
     *
     * @return
     */
    @GetMapping("/subscribe")
    public Result getSubscribe() {
        return Result.ok().data(userService.listSubscribeTypes(UserHolder.get()));
    }

    /**
     * 获取用未订阅的分类
     *
     * @return
     */
    @GetMapping("/noSubscribe")
    public Result getNoSubscribe() {
        return Result.ok().data(userService.listNoSubscribeTypes(UserHolder.get()));
    }

    /**
     * 获取用户上传头像的token
     *
     * @return
     */
    @GetMapping("/avatar/token")
    public Result avatarToken() {
        return Result.ok().data(qiNiuConfig.imageUploadToken());
    }


    /**
     * 获取当前用户所有的收藏夹
     *
     * @return
     */
    @GetMapping("/favorites")
    public Result getUserFavorites() {
        Long userId = UserHolder.get();
        return Result.ok().data(favoritesService.getUserFavorites(userId));
    }

    /**
     * 获取指定收藏夹信息
     *
     * @param id 收藏夹id
     * @return
     */
    @GetMapping("/favorites/{id}")
    public Result geFavorites(@PathVariable Long id) {
        return Result.ok().data(favoritesService.getFavoritesById(id));
    }

    /**
     * 新增/修改收藏夹
     *
     * @param favorites 收藏夹信息
     * @return
     */
    @PostMapping("/favorites")
    public Result saveOrUpdateFavorites(@RequestBody @Validated Favorites favorites) {
        FavoritesVO favoritesVO = favoritesService.saveOrUpdateFavorites(favorites);
        Boolean result = favoritesVO.getResult();
        String message = favoritesVO.getMessage();
        if (!result) {
            return Result.error().message(message);
        }
        return Result.ok().message(message);
    }

    /**
     * 删除收藏夹
     *
     * @param id 收藏夹id
     * @return
     */
    @DeleteMapping("/favorites/{id}")
    public Result deleteFavorites(@PathVariable Long id) {
        Long userId = UserHolder.get();
        Boolean result = favoritesService.deleteFavorites(id, userId);
        if (!result) {
            return Result.error().message("删除失败");
        }

        return Result.ok().message("删除成功");
    }

    /**
     * 修改用户信息
     *
     * @param userVO
     * @return
     */
    @PutMapping
    public Result updateUser(@RequestBody @Validated UpdateUserVO userVO) {
        if (!userService.updateUser(userVO)) {
            return Result.error().message("用户信息修改失败");
        }

        return Result.ok().message("用户信息修改成功");
    }

}
